package com.papercut.gui;



import com.papercut.R;
import com.xtify.android.sdk.PersistentLocationManager;
import com.xtify.android.sdk.PersistentLocationManager.SetUserKeyListener;
import org.androidpn.client.ServiceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Login extends Activity {
        /** Called when the activity is first created. */

        private EditText username_box;
        private EditText password_box;
        private TextView invalid_text;
        private PersistentLocationManager persistentLocationManager;
        private int successful_count;
        private ImageView login_button;
        
    protected View.OnKeyListener onEnterSoftKey = new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                        if (username_box.hasFocus()) {
                                password_box.requestFocus();
                        }
                        else if (password_box.hasFocus()) {
                                login_button.performClick();
                        }
                }
                return true;
       
        }
        
    };

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                successful_count = 0;
                /*
                final Context context = this;

                Thread xtifyThread = new Thread(new Runnable() {
                        @Override
                        public void run() {

                                persistentLocationManager = new PersistentLocationManager(
                                                context);
                                //

                                persistentLocationManager
                                                .setNotificationIcon(R.drawable.notification);
                                persistentLocationManager
                                                .setNotificationDetailsIcon(R.drawable.icon);
                                boolean trackLocation = persistentLocationManager
                                                .isTrackingLocation();
                                boolean deliverNotifications = persistentLocationManager
                                                .isDeliveringNotifications();
                                if (trackLocation || deliverNotifications) {
                                        persistentLocationManager.startService();
                                }
                                Log.d("Device ID",  ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE))
                                                .getDeviceId());
                                persistentLocationManager.setUserKey(
                                                ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE))
                                                .getDeviceId(), new SetUserKeyListener() {

                                                        @Override
                                                        public void onUserKeyRegistered(String arg0,
                                                                        boolean arg1, String arg2) {
                                                                Log.d("login", arg0);
                                                        }

                                                        @Override
                                                        public void onUserKeySet(String arg0) {

                                                        }

                                                });
                        }
                });
                xtifyThread.start();
*/
                SharedPreferences prefs = getSharedPreferences("storage", 0);
                boolean user_configured = prefs.getBoolean("userconfigured", false);
				
                ServiceManager serviceManager = new ServiceManager(this);
                serviceManager.setNotificationIcon(R.drawable.notification);
                
                serviceManager.startService();
                
                if (user_configured) {
                        Intent dash_board_intent = new Intent(Login.this, DashBoard.class);
                        startActivityForResult(dash_board_intent, 30);

                } else {

                        LinearLayout login_view = new LinearLayout(this);
                        login_view.setOrientation(LinearLayout.VERTICAL);
                        login_view.setBackgroundResource(R.drawable.amazon_backgound_login);
                        login_view.setFocusable(true);
                        login_view.setFocusableInTouchMode(true);

                        TextView app_title = new TextView(this);
                        LinearLayout.LayoutParams app_title_layout = new LinearLayout.LayoutParams(
                                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

                        app_title_layout.setMargins(0, 20, 0, 0);
                        app_title.setText("amazon.com hackend");
                        app_title.setTextSize(36);
                        app_title.setTextColor(Color.BLACK);
                        app_title.setGravity(Gravity.CENTER_HORIZONTAL);
                        login_view.addView(app_title, app_title_layout);

                        username_box = new EditText(this);
                        LinearLayout.LayoutParams username_box_layout = new LinearLayout.LayoutParams(
                                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                        username_box_layout.setMargins(90, 60, 90, 0);
                        username_box.setText("Enter Username");
                        username_box.setTextSize(20);
                        username_box.setMaxLines(1);
                        username_box.setTextColor(Color.GRAY);
                        username_box.setOnKeyListener(onEnterSoftKey);
                        username_box.setGravity(Gravity.CENTER_HORIZONTAL);
                        username_box.setOnFocusChangeListener(new OnFocusChangeListener() {

                                @Override
                                public void onFocusChange(View arg0, boolean has_focus) {
                                        if (!has_focus
                                                        && (username_box.getText().length() == 0 || username_box
                                                                        .getText().toString()
                                                                        .equals("Enter Username:"))) {
                                                username_box.setTextColor(Color.GRAY);
                                                username_box.setText("Enter Username:");

                                        }
                                        if (has_focus == true) {
                                                username_box.setText("");
                                                username_box.setTextColor(Color.BLACK);
                                        }
                                }

                        });
                        login_view.addView(username_box, username_box_layout);

                        password_box = new EditText(this);
                        LinearLayout.LayoutParams password_box_layout = new LinearLayout.LayoutParams(
                                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                        password_box_layout.setMargins(90, 30, 90, 0);
                        password_box.setText("Enter password:");
                        password_box.setTextSize(20);
                        password_box.setMaxLines(1);
                        password_box.setTextColor(Color.GRAY);
                        password_box.setGravity(Gravity.CENTER_HORIZONTAL);
                        password_box.setOnKeyListener(onEnterSoftKey);
                        password_box.setOnFocusChangeListener(new OnFocusChangeListener() {

                                @Override
                                public void onFocusChange(View arg0, boolean has_focus) {
                                        if (!has_focus
                                                        && password_box.getText().length() == 0
                                                        || password_box.getText().toString()
                                                                        .equals("Enter Password:")) {
                                                password_box.setText("Enter Password:");
                                                password_box.setTextColor(Color.GRAY);

                                        }
                                        if (has_focus) {
                        password_box.setText("");
                                                password_box.setTextColor(Color.BLACK);
                                        }
                                }

                        });
                        login_view.addView(password_box, password_box_layout);

                        invalid_text = new TextView(this);
                        LinearLayout.LayoutParams invalid_text_layout = new LinearLayout.LayoutParams(
                                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                        invalid_text_layout.setMargins(0, 70, 0, 0);
                        invalid_text.setText("Invalid Credentials");
                        invalid_text.setTextSize(20);
                        invalid_text.setTextColor(Color.RED);
                        invalid_text.setGravity(Gravity.CENTER_HORIZONTAL);
                        invalid_text.setVisibility(TextView.INVISIBLE);
                        login_view.addView(invalid_text, invalid_text_layout);

                        login_button = new ImageView(this);
                        LinearLayout.LayoutParams login_button_layout = new LinearLayout.LayoutParams(
                                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        login_button_layout.setMargins(125, 175, 125, 0);
                        login_button.setVisibility(View.VISIBLE);
                        login_button.setImageResource(R.drawable.login_button);
                        login_button.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View arg0) {
                                	if (successful_count < 1) {
                                		String username = username_box.getText().toString().trim();
                                        String password = password_box.getText().toString().trim();
                                        // validate username and password, hardcoded in right now
                                        if (!password.equals("") && !username.equals("")) {
                                        	successful_count++;
                                        	
                                                invalid_text.setVisibility(TextView.INVISIBLE);
                                                Intent swipe_check_intent = new Intent(Login.this,
                                                                SwipeCheck.class);
                                                startActivityForResult(swipe_check_intent, 20);

                                        } else {
                                                invalid_text.setVisibility(TextView.VISIBLE);
                                        }
                                	}
                                }

                        });
                        login_view.addView(login_button, login_button_layout);

                        setContentView(login_view);

                }

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                finish();
        }

}